#!/bin/bash

# Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.


# @author Augusto Monteiro
#
# This script assists in the installation and management of
# skills loaded from Github.  

mycroft_skill_folder="/opt/mycroft/skills"
mycroft_virtualenv=~/.virtualenvs/mycroft/bin/activate

echo "#######  Mycroft Skill Manager #######"

function help() {
	echo "msm: Mycroft Skill Manager"
	echo -e "     Copyright (c) 2017 Mycroft AI, Inc.  All rights reserved.\n"
	echo "usage: msm install <repository> or <name>"
	echo "     Installs the given Skill into the /opt/mycroft/skills directory"
	echo "     where <repository> is the address of the skill in Github."
	echo -e "example: msm install https://github.com/ethanaward/demo_skill.git\n"
}

function install() {
        cd $mycroft_skill_folder
	if [ -z "$2" ]; then
		echo "You must pass the git url or skill name"
		exit 1
	fi
	if [[ "$2" == "git@"* || "$2" == "https://"* || "$2" == "http://"* ]]; then
		repo=$2
	else
		skill_list="`curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules"`"
		skills=`echo "$skill_list" | grep -n 'submodule' | sed 's/[[:space:]]//g' | sed 's/\[submodule"//g' | sed 's/"\]//g'`
		exact_match=`echo "$skills" | grep -i ".*:$2$"`
		skill=`echo "$skills" | grep -i ".*:.*$2.*"`
		if [ ! -z $exact_match ]; then
			skill=$exact_match
		fi
		git_line=`echo "$skill" | sed 's/\:.*//'`

		if [[ $skill ==  *$'\n'* ]]; then
			echo -e "Your search has multiple choices\n\n$skill" | sed 's/.*://g'
			exit 2
		else
			if [ -z $git_line ]; then
				echo "Skill not found"
				exit 3
			fi
			repo_line=$(($git_line + 2))
			repo=`echo "$skill_list" | sed -n $repo_line'{p;q;}' | sed 's/[[:space:]]//g' | sed 's/url=//g'`
		fi 
	fi
	git_name=`echo "$repo" | sed 's/.*\///'`
	name=`echo "$git_name" | sed 's/.git//'`
        echo "Cloning repository"
        git clone $repo >> /dev/null
        cd $name
         if [ -f "requirements.txt" ]; then
         	echo "Installing libraries requirements"
        	pip install -r requirements.txt
        fi 
        echo "Skill installed!"
}

function update() {
        cd $mycroft_skill_folder
	for d in $(ls -d */); do
		if git -C "$d" rev-parse --git-dir > /dev/null 2>&1; then
			cd $d
			if [[ -z $(git status --porcelain) ]]; then
				git fetch
				git reset --hard origin/master 
			fi
			cd ..
		fi
	done
}

function install_defaults() {
	skills=( "alarm" "audio-record" "configuration" "date-time" "desktop-launcher" "ip" "joke" "hello-world" "media" "npr-news" "naptime" "pairing" "personal" "reminder" "installer" "singing" "speak" "spelling" "stop" "stock" "volume" "weather" "wiki" "wolfram-alpha" "mark1-demo" )
	for i in "${skills[@]}"
	do
		if [ ! -d "$mycroft_skill_folder/skill-$i" ]; then
			install "" "https://github.com/MycroftAI/skill-$i.git"	
		fi
	done
	update
	echo "Installed!"
	exit 0
}

function list() {
	curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules" | grep 'submodule "' | sed 's/\[submodule "//g'| sed 's/"\]//g'
}

if [ "$1" = "install" ]; then
	install $*
elif [ "$1" = "list" ]; then
	echo -e "Searching...\n"
	list 
elif [ "$1" = "update" ]; then
	update 
elif [ "$1" = "default" ]; then
	install_defaults 
else
	help
fi

